<?php

namespace App\Task;


class test2 extends TaskBase
{
    public function funcHandle($index)
    {
//        $s = $this->db()->rawQuery('SELECT SLEEP(1)');

//        \co::sleep(1);
//        var_dump($data);

        $table_name = 'wx_app.t_t_test';

        $list = $this->db()
            ->where('t_handle', 0)
            ->get($table_name, [$index, 1]);
        if (count($list) <= 0)
        {
            $r = $this->redis()->get('task|update|' . \h::today()->toDateString());
            if (\h::funcStrIsNullOrEmpty($r))
            {
                $arr_first = $this->db()
                    ->where('t_handle_date', \h::today()->toDateString(), '>')
                    ->getOne($table_name);
                $arr_last = $this->db()
                    ->orderBy('t_handle_date')
                    ->getOne($table_name);

                $begin_time = \Carbon::parse($arr_first['t_handle_date']);
                $end_time = \Carbon::parse($arr_last['t_handle_date']);

                $use_s = $end_time->diffInSeconds($begin_time);

                $this->redis()->set('task|update|' . \h::today()->toDateString(), "使用时间：{$use_s}秒");
            }

            return 0;
        }

        $entity = $list[0];

        $this->db()->where('t_id', $entity['t_id'])
            ->update($table_name, [
                't_handle' => 1,
                't_handle_date' => \h::now()->toDateTimeString(),
            ]);


        return 1;
    }


    public function funcDone()
    {
        \h::funcLog(__METHOD__, 'funcDone');
    }

}